package com.elective.courseselect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elective.courseselect.dao.StudentCharacterDao;
import com.elective.courseselect.entity.StudentCharacterEntity;
import com.elective.courseselect.entity.StudentEntity;
import com.elective.courseselect.service.StudentCharacterService;
import com.elective.courseselect.utils.PageUtils;
import com.elective.courseselect.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Map;



@Service("studentCharacterService")
public class StudentCharacterServiceImpl extends ServiceImpl<StudentCharacterDao, StudentCharacterEntity> implements StudentCharacterService {

    @Autowired
    StudentCharacterDao studentCharacterDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StudentCharacterEntity> page = this.page(
                new Query<StudentCharacterEntity>().getPage(params),
                new QueryWrapper<StudentCharacterEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStuChar(StudentCharacterEntity studentCharacter) {
        QueryWrapper<StudentCharacterEntity> wrapper = new QueryWrapper();
        wrapper.eq("student_id", studentCharacter.getStudentId());
        StudentCharacterEntity one = this.getOne(wrapper);
        if (null == one) {
            this.save(studentCharacter);
        }else{
            this.update(studentCharacter, wrapper);
        }
    }


    /**
     * Application构建查询对象
     * @param studentCharacterEntity
     * @return
     */
    public static QueryWrapper createExample(StudentCharacterEntity studentCharacterEntity){
        QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
        if(studentCharacterEntity!=null){
            //
            if(!StringUtils.isEmpty(studentCharacterEntity.getId())){
                wrapper.eq("id",studentCharacterEntity.getId());
            }
            //
            if(!StringUtils.isEmpty(studentCharacterEntity.getStudentId())){
                wrapper.eq("student_id",studentCharacterEntity.getStudentId());
            }
            if(!StringUtils.isEmpty(studentCharacterEntity.getCharacterId())){
                wrapper.eq("character_id",studentCharacterEntity.getCharacterId());
            }
        }
        return wrapper;
    }

}